

package com.hazelcast.internal.metrics.impl;

import com.hazelcast.instance.impl.OutOfMemoryErrorDispatcher;
import com.hazelcast.internal.metrics.MetricDescriptor;
import com.hazelcast.internal.metrics.MetricsPublisher;
import com.hazelcast.internal.metrics.collectors.MetricsCollector;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;

/**
 * {@link MetricsCollector} implementation delegating to the configured
 * publishers.
 */
public class PublisherMetricsCollector implements MetricsCollector {
    private final ILogger logger = Logger.getLogger(PublisherMetricsCollector.class);

    private final MetricsPublisher[] publishers;

    public PublisherMetricsCollector(MetricsPublisher... publishers) {
        this.publishers = publishers;
    }

    public void publishCollectedMetrics() {
        for (int i = 0; i < publishers.length; i++) {
            try {
                publishers[i].whenComplete();
            } catch (OutOfMemoryError e) {
                OutOfMemoryErrorDispatcher.onOutOfMemory(e);
            } catch (Throwable throwable) {
                logger.severe("Error completing publication for publisher " + publishers[i].name(), throwable);
            }
        }
    }

    public void shutdown() {
        for (int i = 0; i < publishers.length; i++) {
            try {
                publishers[i].shutdown();
            } catch (OutOfMemoryError e) {
                OutOfMemoryErrorDispatcher.onOutOfMemory(e);
            } catch (Throwable throwable) {
                logger.severe("Error shutting down metrics publisher " + publishers[i].name(), throwable);
            }
        }
    }

    @Override
    public void collectLong(MetricDescriptor descriptor, long value) {
        for (int i = 0; i < publishers.length; i++) {
            try {
                publishers[i].publishLong(descriptor, value);
            } catch (OutOfMemoryError e) {
                OutOfMemoryErrorDispatcher.onOutOfMemory(e);
            } catch (Throwable throwable) {
                logError(descriptor, value, publishers[i], throwable);
            }
        }
    }

    @Override
    public void collectDouble(MetricDescriptor descriptor, double value) {
        for (int i = 0; i < publishers.length; i++) {
            try {
                publishers[i].publishDouble(descriptor, value);
            } catch (OutOfMemoryError e) {
                OutOfMemoryErrorDispatcher.onOutOfMemory(e);
            } catch (Throwable throwable) {
                logError(descriptor, value, publishers[i], throwable);
            }
        }
    }

    @Override
    public void collectException(MetricDescriptor descriptor, Exception e) {
        logger.warning("Error when collecting '" + descriptor.toString() + '\'', e);
    }

    @Override
    public void collectNoValue(MetricDescriptor descriptor) {
        // noop
    }

    private void logError(MetricDescriptor descriptor, Object value, MetricsPublisher publisher, Throwable throwable) {
        logger.fine("Error publishing metric to: " + publisher.name() + ", metric=" + descriptor.toString() + ", value=" + value, throwable);
    }

}
